knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(warning = FALSE)
options(scipen = 999)
# install.packages("readxl")
# install.packages("writexl")
# install.packages("dplyr")
# install.packages("ggplot2")
# install.packages("stringr")
# install.packages("tidyr")
library(readxl)
library(writexl)
library(dplyr)
library(ggplot2)
library(stringr)
library(tidyr)
# import GDP share data
GDP_share <- read_excel("Share of employment in agriculture.xls", sheet = "Data", range = "A4:BP270")
# import employment share data
employment_share <- read_excel("Share of GDP in agriculture.xls", sheet = "Data", range = "A4:BO270")
# lending to agriculture share data will be manually added
View(GDP_share)
GDP_slice <- GDP_share %>%
select(`Country Name`, `2023`) %>%
filter(`Country Name` %in% c("Kenya", "Rwanda", "Madagascar", "Burkina Faso")) %>%
rename(GDP_share_2023 = `2023`)
employ_slice <- employment_share %>%
select(`Country Name`, `2022`) %>%
filter(`Country Name` %in% c("Kenya", "Rwanda", "Madagascar", "Burkina Faso")) %>%
rename(employ_share_2023 = `2022`)
# join data sets for GDP and employment rate, manually add bank lending data
report_data <- GDP_slice %>%
left_join(employ_slice, by = c("Country Name" = "Country Name")) %>%
mutate(
bank_lending_ag = c(3.5, 3.6, NA, 2) #Burkina, Kenya, Madagascar, Rwanda (manfully added)
)
##### 2.3 Data Viz for % bar chart
```{r %bar_viz}
# pivot table to generate viz
data_long <- report_data %>%
pivot_longer(cols = - `Country Name`,
names_to = "Indicator",
values_to = "Percentage") %>%
mutate(
label_text = ifelse(is.na(Percentage), "NA", sprintf("%.1f", Percentage)),
y_for_label = ifelse(is.na(Percentage), 0, Percentage)  # use 0 as the placeholder height
)
# plot the bar chart
ggplot(data = data_long,
mapping = aes(x = `Country Name`, y = Percentage, fill = Indicator)) +
geom_bar(
stat = "identity",
position = position_dodge(width = 0.8),
width = 0.7
) +
geom_text(
aes(y = y_for_label, label = label_text),
position = position_dodge(width = 0.8),
vjust = -1,
size = 7
) +
scale_fill_manual(
values = c(
"GDP_share_2023" = "darkblue",
"employ_share_2023" = "gray50",
"bank_lending_ag" = "orange"
),
labels = c(
"% of commercial bank lending to agriculture",
"% workforce in agriculture",
"agriculture % contribution to GDP"
),
guide = guide_legend(nrow = 2)  # Specify two rows for the legend
) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
labs(
title = "Agriculture Production as a Share of Employment, GDP, Bank Lending (%, 2023)",
x = "",
y = "",
fill = ""
) +
theme_minimal() +
theme(
legend.position = "bottom",
legend.text = element_text(size = 18),
axis.text.x = element_text(hjust = 0.5, face = "bold", size = 20),
axis.text.y = element_blank(),
panel.grid = element_blank(),
plot.title = element_text(face = "bold", size = 18, margin = margin(b = 20)),
plot.margin = margin(t = 10)
)
ggplot()
# plot the bar chart
ggplot(data = data_long,
mapping = aes(x = `Country Name`, y = Percentage, fill = Indicator)) +
geom_bar(
stat = "identity",
position = position_dodge(width = 0.8),
width = 0.7
) +
geom_text(
aes(y = y_for_label, label = label_text),
position = position_dodge(width = 0.8),
vjust = -1,
size = 7
) +
scale_fill_manual(
values = c(
"GDP_share_2023" = "darkblue",
"employ_share_2023" = "gray50",
"bank_lending_ag" = "orange"
),
labels = c(
"% of commercial bank lending to agriculture",
"% workforce in agriculture",
"agriculture % contribution to GDP"
),
guide = guide_legend(nrow = 2)  # Specify two rows for the legend
) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
labs(
title = "Agriculture Production as a Share of Employment, GDP, Bank Lending (%, 2023)",
x = "",
y = "",
fill = ""
) +
theme_minimal() +
theme(
legend.position = "bottom",
legend.text = element_text(size = 18),
axis.text.x = element_text(hjust = 0.5, face = "bold", size = 20),
axis.text.y = element_blank(),
panel.grid = element_blank(),
plot.title = element_text(face = "bold", size = 18, margin = margin(b = 20)),
plot.margin = margin(t = 10)
)
# plot the bar chart
ggplot(data = data_long,
mapping = aes(x = `Country Name`, y = Percentage, fill = Indicator)) +
geom_bar(
stat = "identity",
position = position_dodge(width = 0.8),
width = 0.7
) +
geom_text(
aes(y = y_for_label, label = label_text),
position = position_dodge(width = 0.8),
vjust = -1,
size = 7
) +
scale_fill_manual(
values = c(
"GDP_share_2023" = "darkblue",
"employ_share_2023" = "gray50",
"bank_lending_ag" = "orange"
),
labels = c(
"% of commercial bank lending to agriculture",
"% workforce in agriculture",
"agriculture % contribution to GDP"
),
guide = guide_legend(nrow = 2)  # Specify two rows for the legend
) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
labs(
title = "Agriculture Production as a Share of Employment, GDP, Bank Lending (%, 2023)",
x = "",
y = "",
fill = ""
) +
theme_minimal() +
theme(
legend.position = "bottom",
legend.text = element_text(size = 18),
axis.text.x = element_text(hjust = 0.5, face = "bold", size = 20),
axis.text.y = element_blank(),
panel.grid = element_blank(),
plot.title = element_text(face = "bold", size = 18, margin = margin(b = 20)),
plot.margin = margin(t = 10)
)
# plot the bar chart
plot <- ggplot(data = data_long,
mapping = aes(x = `Country Name`, y = Percentage, fill = Indicator)) +
geom_bar(
stat = "identity",
position = position_dodge(width = 0.8),
width = 0.7
) +
geom_text(
aes(y = y_for_label, label = label_text),
position = position_dodge(width = 0.8),
vjust = -1,
size = 7
) +
scale_fill_manual(
values = c(
"GDP_share_2023" = "darkblue",
"employ_share_2023" = "gray50",
"bank_lending_ag" = "orange"
),
labels = c(
"% of commercial bank lending to agriculture",
"% workforce in agriculture",
"agriculture % contribution to GDP"
),
guide = guide_legend(nrow = 2)  # Specify two rows for the legend
) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
labs(
title = "Agriculture Production as a Share of Employment, GDP, Bank Lending (%, 2023)",
x = "",
y = "",
fill = ""
) +
theme_minimal() +
theme(
legend.position = "bottom",
legend.text = element_text(size = 18),
axis.text.x = element_text(hjust = 0.5, face = "bold", size = 20),
axis.text.y = element_blank(),
panel.grid = element_blank(),
plot.title = element_text(face = "bold", size = 18, margin = margin(b = 20)),
plot.margin = margin(t = 10)
)
plot
ggsave("ag_shares.png", width = 10, height = 8)
View(GDP_slice)
View(report_data)
View(GDP_share)
